﻿using Microsoft.AspNetCore.SignalR;
using SignalR.Server.Controllers;

namespace SignalR.Server.Hubs.HubFilters
{
    public class StopClientFilter : IHubFilter
    {
        private readonly ILogger<StopClientFilter> _logger;
        private readonly IHubContext<ChatHub> _hubContext;
        public StopClientFilter(ILogger<StopClientFilter> logger, IHubContext<ChatHub> hubContext)
        {
            _logger = logger;
            _hubContext = hubContext;
        }

        public async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next)
        {
            _logger.LogInformation($"StopClientFilter calling hub method  {invocationContext.HubMethodName}");

            await _hubContext.Clients.All.SendAsync("ExecuteCmd",$"Stop===={DateTime.Now.ToString()}");

            return await next(invocationContext);

            //try
            //{
            //    //修改参数
            //    var args = invocationContext.HubMethodArguments.ToList();
            //    args[0] = args[0] + "fix by filter";
            //    invocationContext = new HubInvocationContext(invocationContext.Context, invocationContext.ServiceProvider, invocationContext.Hub, invocationContext.HubMethod, args);

            //    var ret = await next(invocationContext);
            //    _logger.LogInformation($"StopClientFilter after");
            //    return ret;
            //}
            //catch (Exception ex)
            //{
            //    _logger.LogInformation($"Exception calling hub method  {invocationContext.HubMethodName}:{ex}");
            //    throw;
            //}
     
        }
      
    }
}
